package com.hly.basickotlin
import android.graphics.Color
import android.os.Bundle
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentTransaction
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.hly.basickotlin.fragment.MeFragment
import com.hly.basickotlin.fragment.NewsFragment
import com.hly.basickotlin.fragment.SetFragment
import com.hly.basickotlin.fragment.SquareFragment
class MainActivity : AppCompatActivity(), View.OnClickListener {

    private var meFragment: MeFragment? = null
    private var squareFragment: SquareFragment? = null
    private var setFragment: SetFragment? = null
    private var newsFragment: NewsFragment? = null

    private var meimage: ImageView? = null
    private var squareimage: ImageView? = null
    private var setimage: ImageView? = null
    private var newsimage: ImageView? = null

    private var melayout: View? = null
    private var squarelayout: View? = null
    private var setlayout: View? = null
    private var newslayout: View? = null


    private var meText: TextView? = null
    private var squareText: TextView? = null
    private var setText: TextView? = null
    private var newsText: TextView? = null

    private var fragmentManager: FragmentManager? = null//对fragment的管理

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        fragmentManager = supportFragmentManager
        initView()
        setTabSelection(0)//第一次启动默认的tab(0)
    }

    private fun initView() {
        melayout = findViewById(R.id.me_bottom_layout)
        meimage = findViewById(R.id.me_bottom_image)
        meText = findViewById(R.id.me_bottom_text)

        squarelayout = findViewById(R.id.square_bottom_layout)
        squareimage = findViewById(R.id.square_bottom_image)
        squareText = findViewById(R.id.square_bottom_text)

        setlayout = findViewById(R.id.set_bottom_layout)
        setimage = findViewById(R.id.set_bottom_image)
        setText = findViewById(R.id.set_bottom_text)

        newslayout = findViewById(R.id.news_bottom_layout)
        newsimage = findViewById(R.id.news_bottom_image)
        newsText = findViewById(R.id.news_bottom_text)

        melayout!!.setOnClickListener(this)
        squarelayout!!.setOnClickListener(this)
        setlayout!!.setOnClickListener(this)
        newslayout!!.setOnClickListener(this)

    }

    fun setTabSelection(index: Int) {
        val transaction = fragmentManager!!.beginTransaction()
        clearSelection()//每次选中之前清除掉上次选中的状态
        hideFragments(transaction)
        when (index) {
            0 -> {
                meimage!!.setImageResource(R.mipmap.tab_contact_select)
                meText!!.setTextColor(Color.parseColor("#293754"))
                melayout!!.setBackgroundColor(-0x201e1a)
                if (meFragment == null) {
                    meFragment = MeFragment()
                    transaction.add(R.id.content, meFragment)
                } else {
                    transaction.show(meFragment)
                }
            }
            1 -> {
                squareimage!!.setImageResource(R.mipmap.tab_home_select)
                squareText!!.setTextColor(Color.parseColor("#293754"))
                squarelayout!!.setBackgroundColor(-0x201e1a)
                if (squareFragment == null) {
                    squareFragment = SquareFragment()
                    transaction.add(R.id.content, squareFragment)
                } else {
                    transaction.show(squareFragment)
                }
            }
            2 -> {
                setimage!!.setImageResource(R.mipmap.tab_more_select)
                setText!!.setTextColor(Color.parseColor("#293754"))
                setlayout!!.setBackgroundColor(-0x201e1a)
                if (setFragment == null) {
                    setFragment = SetFragment()
                    transaction.add(R.id.content, setFragment)
                } else {
                    transaction.show(setFragment)
                }
            }

            3 -> {
                newsimage!!.setImageResource(R.mipmap.tab_speech_select)
                newsText!!.setTextColor(Color.parseColor("#293754"))
                newslayout!!.setBackgroundColor(-0x201e1a)
                if (newsFragment == null) {
                    newsFragment = NewsFragment()
                    transaction.add(R.id.content, newsFragment)
                } else {
                    transaction.show(newsFragment)
                }
            }
        }
        transaction.commitAllowingStateLoss()

    }

    /**
     * 清除所有选中的状态
     */
    private fun clearSelection() {
        meimage!!.setImageResource(R.mipmap.tab_contact_unselect)
        meText!!.setTextColor(Color.parseColor("#82858b"))
        melayout!!.setBackgroundColor(-0x1)
        squareimage!!.setImageResource(R.mipmap.tab_home_unselect)
        squareText!!.setTextColor(Color.parseColor("#82858b"))
        squarelayout!!.setBackgroundColor(-0x1)
        setimage!!.setImageResource(R.mipmap.tab_more_unselect)
        setText!!.setTextColor(Color.parseColor("#82858b"))
        setlayout!!.setBackgroundColor(-0x1)
        newsimage!!.setImageResource(R.mipmap.tab_speech_unselect)
        newsText!!.setTextColor(Color.parseColor("#82858b"))
        newslayout!!.setBackgroundColor(-0x1)
    }

    /**
     * 将所有的Fragment设置隐藏状态,用于对Fragment执行操作的事务
     *
     * @param transaction
     */
    private fun hideFragments(transaction: FragmentTransaction) {
        if (meFragment != null) {
            transaction.hide(meFragment)
        }
        if (squareFragment != null) {
            transaction.hide(squareFragment)
        }
        if (setFragment != null) {
            transaction.hide(setFragment)
        }
        if (newsFragment != null) {
            transaction.hide(newsFragment)
        }
    }


    override fun onClick(view: View) {
        when (view.id) {
            R.id.me_bottom_layout -> setTabSelection(0)
            R.id.square_bottom_layout -> setTabSelection(1)
            R.id.set_bottom_layout -> setTabSelection(2)
            R.id.news_bottom_layout -> setTabSelection(3)
        }

    }


}




